﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MemoLib
{
    public class MemoBook
    {
        public string BookName { set; get; }
        public int BookID { set; get; }
        public string BookUID { set; get; }
        public string BookPublisher { set; get; }
        public string BookPublishDate { set; get; }
        public int LessonTotal;
        public int ItemTotal;

        DBContext database;

        public MemoBook( int bookID)
        {
            database = MemoBookConfig.GetDBContext(bookID);
        }

        public static MemoBook Instance ( int bookID)
        {
            return (new MemoBook ( bookID ));
        }

        public MemoItem GetItemByID( int itemID)
        {
            var item = database.MemoItem.Where(c => c.ItemID == itemID).SingleOrDefault();
            return item;
        }

        public static List<MemoItem> GetItemListByLesson(int bookID, int lessonID)
        {
            //if lesson = "" then all
            List<MemoItem> list = new List<MemoItem>();

            DBContext db = MemoBookConfig.GetDBContext(bookID);
            var items = db.MemoItem.Where(c => c.Book == bookID && c.Category == lessonID);

            foreach (var item in items)
            {
                list.Add(item);
            }
            return list;
            
        }

        public static List<MemoItem> GetItemListAll(int bookID)
        {
            //if lesson = "" then all
            List<MemoItem> list = new List<MemoItem>();

            DBContext db = MemoBookConfig.GetDBContext(bookID);
            var items = db.MemoItem.Where(c => c.Book == bookID );

            foreach (var item in items)
            {
                list.Add(item);
            }
            return list;

        }

        public List<MemoItem> GetItemListByLesson(int lessonID)
        {
            List<MemoItem> list = new List<MemoItem>();

            var items = database.MemoItem.Where(c => c.Category == lessonID);

            foreach (var item in items)
            {
                list.Add(item);
            }
            return list;
        }

        public List<MemoItem> GetItemList(int start, int end)
        {
            //if lesson = "" then all
            List<MemoItem> list = new List<MemoItem>();

            var item = new MemoItem { };
            list.Add(item);

            return list;
        }

        public int SaveItem(MemoLib.MemoItem item)
        {
            int rc = 0;
            if (item == null)
                return rc;

            //int itemID = _Item
            MemoLib.MemoItem ri = database.MemoItem.Where(c => c.ItemID == item.ItemID).FirstOrDefault();
            if (ri == null)
            {
                //ri.ItemID = item.ItemID;
                ri.Category = item.Category;
                ri.Book = item.Book;
                ri.ItemAnswer = item.ItemAnswer;
                ri.ItemDesc = item.ItemDesc;
                ri.ItemStatus = item.ItemStatus;
                ri.ItemFlow = item.ItemFlow;

                ri.ItemName = item.ItemName;
                ri.ItemOrder = item.ItemOrder;
                ri.ItemPicture = item.ItemPicture;
                ri.ItemQuestion = item.ItemQuestion;
                ri.ItemRelation = item.ItemRelation;
                ri.ItemSound = item.ItemSound;
                ri.ItemSource = item.ItemSource;
                ri.ItemType = item.ItemType;
                ri.ItemVideo = item.ItemVideo;

                database.MemoItem.Add(ri);
            }
            else
            {
                ri.ItemID = item.ItemID;
                ri.Category = item.Category;
                ri.Book = item.Book;
                ri.ItemAnswer = item.ItemAnswer;
                ri.ItemDesc = item.ItemDesc;
                ri.ItemStatus = item.ItemStatus;
                ri.ItemFlow = item.ItemFlow;

                ri.ItemName = item.ItemName;
                ri.ItemOrder = item.ItemOrder;
                ri.ItemPicture = item.ItemPicture;
                ri.ItemQuestion = item.ItemQuestion;
                ri.ItemRelation = item.ItemRelation;
                ri.ItemSound = item.ItemSound;
                ri.ItemSource = item.ItemSource;
                ri.ItemType = item.ItemType;
                ri.ItemVideo = item.ItemVideo;
            }

            rc = database.SaveChanges();
            return rc;
        }
    }
}
